#include "TCP_SYS.h"

extern UART_HandleTypeDef hlpuart1;
extern UART_HandleTypeDef huart3;
extern uint8_t		Upload_Buf[256];
extern uint8_t	Register_Buf[255];
extern uint8_t Register_Value[255];
extern Value_Flag VFlag;
extern Value_64 V64;
extern Value_32 V32;
extern uint8_t Task;
extern uint64_t    TCP_Time;
uint8_t Fun_Flag_End;

void Upload_Config(void)
{
	Upload_Buf[0] = VFlag.Gather_Cnt;
	Upload_Buf[1]	= Frame_Head;
	
	V32.Data = Slave_Address;
	Upload_Buf[2]	= V32.Buf[3];
	Upload_Buf[3]	= V32.Buf[2];
	Upload_Buf[4]	= V32.Buf[1];
	Upload_Buf[5]	= V32.Buf[0];
		
	Upload_Buf[6]	= Fun_Read;
	Upload_Buf[7]	= VFlag.Register_Cnt * 4 + 18;
	
	Upload_Buf[8]	= 0;
	Upload_Buf[9]	= 0;
	Upload_Buf[10]	= 0;
	Upload_Buf[11]	= 0;
	Upload_Buf[12]	= 0;
	Upload_Buf[13]	= 0;
	
	if(Get_ADC(V24) > 20000)		//20V
	{
		Upload_Buf[14]	= Get_ADC(V24) >> 8;
		Upload_Buf[15]	= Get_ADC(V24);
	}else{
		Upload_Buf[14]	= Get_ADC(BAT) >> 8;
		Upload_Buf[15]	= Get_ADC(BAT);
	}
		
	Upload_Buf[16]	= VFlag.Signal_Strength;
	Upload_Buf[17]	= VFlag.Pre_Gather_Way;
	//todo
	
	V64.Data = VFlag.Pre_Value;
	Upload_Buf[18]	= V64.Buf[3];
	Upload_Buf[19]	= V64.Buf[2];
	Upload_Buf[20]	= V64.Buf[1];
	Upload_Buf[21]	= V64.Buf[0];
	
	V64.Data = Get_ADC(PT100);
	Upload_Buf[22]	= V64.Buf[3];
	Upload_Buf[23]	= V64.Buf[2];
	Upload_Buf[24]	= V64.Buf[1];
	Upload_Buf[25]	= V64.Buf[0];
	
	for(uint8_t a = 0; a < (VFlag.Register_Cnt); a ++)
	{
		Upload_Buf[26 + a * 4] = Register_Value[4 * a];
		Upload_Buf[27 + a * 4] = Register_Value[4 * a + 1];
		Upload_Buf[28 + a * 4] = Register_Value[4 * a + 2];
		Upload_Buf[29 + a * 4] = Register_Value[4 * a + 3];
	}
	
	Upload_Buf[VFlag.Register_Cnt * 4 + 26]	= CRC16_MODBUS(&Upload_Buf[2], VFlag.Register_Cnt * 4 + 28) >> 8;
	Upload_Buf[VFlag.Register_Cnt * 4 + 27]	= CRC16_MODBUS(&Upload_Buf[2], VFlag.Register_Cnt * 4 + 28);
	Upload_Buf[VFlag.Register_Cnt * 4 + 28]	= Frame_Trail;

}


void Fun_Order(uint8_t Fun_Value)
{

	Upload_Buf[0] 	= Frame_Head;
	
	V64.Data = Slave_Address;
	Upload_Buf[1]	= V64.Buf[3];
	Upload_Buf[2]	= V64.Buf[2];
	Upload_Buf[3]	= V64.Buf[1];
	Upload_Buf[4]	= V64.Buf[0];
	
	Upload_Buf[5]	= Fun_Value;
	
	Upload_Buf[6]	= CRC16_MODBUS(&Upload_Buf[1], 5);
	Upload_Buf[7]	= CRC16_MODBUS(&Upload_Buf[1], 5) >> 8;
	
	Upload_Buf[8]	= Frame_Trail;

}


uint8_t SYS_Process(uint8_t buf[])
{
	static uint8_t Len;
	static uint8_t Fun;
	static uint8_t Cnt;
	static char at_cmd[100];
	
	Len = buf[6];
	Fun = buf[5];
	
	if(buf[Len * 4 + 25] == CRC16_MODBUS(&buf[1], Len * 4 + 24) && \
		buf[Len * 4 + 26] == CRC16_MODBUS(&buf[1], Len * 4 + 24) >> 8)
	{
		return 1;
	}else 
	{
		if(strstr((const char*)buf,(const char*)"ERROR"))return 0;
		if(strstr((const char*)buf,(const char*)"CSQ"))
		{
			if(buf[16] == ',')VFlag.Signal_Strength = buf[15];
			else VFlag.Signal_Strength = (buf[15] - 0x30) * 10 + buf[16] - 0x30 ;
			sprintf(at_cmd, "SIGNAL STRENGTH : %d \r\n",VFlag.Signal_Strength);
			HAL_UART_Transmit(&huart3, (uint8_t *)at_cmd, strlen(at_cmd), 30);
			Delay_ms(100);
			if(VFlag.Signal_Strength > 0 && VFlag.Signal_Strength < 32 \
				&& VFlag.Signal_Strength != 99)return 1;
			else return 0;
		}
		if(strstr((const char*)buf,(const char*)"QLTS"))
		{
			V64.Buf[5] = (buf[22] - 0x30) * 10 + buf[23] - 0x30 ;
			V64.Buf[4] = (buf[25] - 0x30) * 10 + buf[26] - 0x30 ;
			V64.Buf[3] = (buf[28] - 0x30) * 10 + buf[29] - 0x30 ;
			V64.Buf[2] = (buf[31] - 0x30) * 10 + buf[32] - 0x30 ;
			V64.Buf[1] = (buf[34] - 0x30) * 10 + buf[35] - 0x30 ;
			V64.Buf[0] = (buf[37] - 0x30) * 10 + buf[38] - 0x30 ;

			TCP_Time = V64.Data;
		}
		if(strstr((const char*)buf,(const char*)"OK"))return 1;
		if(strstr((const char*)buf,(const char*)"QISEND"))return 1;
		
		
	}	
return 0;
	
}



